from pymouse import PyMouse
from pykeyboard import PyKeyboard
import time
import win32api
import win32con



def run(url,i):
    myMouse=PyMouse()
    k=PyKeyboard()

    #点击网页
    time.sleep(0.5)
    myMouse.click(99,17, 1)


    #输入网址
    time.sleep(0.5)
    myMouse.click(1389, 54,1)
    k.press_key(k.backspace_key)
    time.sleep(1)
    k.release_key(k.backspace_key)
    k.type_string(url)
    time.sleep(0.1)
    k.press_key(k.enter_key)
    time.sleep(0.01)
    k.release_key(k.enter_key)



    #进入网站
    time.sleep(2)
    k.press_key(k.enter_key)
    time.sleep(0.01)
    k.release_key(k.enter_key)



    time.sleep(5)
    myMouse.click(242, 277,1)

    time.sleep(1.5)
    myMouse.click(603, 537,1)
    time.sleep(1)
    myMouse.click(1519, 802)
    time.sleep(1)
    #鼠标滚动
    win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, 1000)
    time.sleep(0.1)
    myMouse.move(603, 537)
    time.sleep(0.1)
    myMouse.move(745, 777)
    time.sleep(0.1)
    myMouse.click(745, 777,1)
    time.sleep(1)
    myMouse.click(614, 711,1)
    time.sleep(2.5)
    k.press_key(k.shift_l_key)
    time.sleep(0.01)
    k.release_key(k.shift_l_key)
    k.type_string('all_'+str(i))
    k.press_key(k.enter_key)
    time.sleep(0.01)
    k.release_key(k.enter_key)

    #关闭网页
    time.sleep(1)
    myMouse.click(470,18,1)



def read(path):
    with open(path,'r',encoding='utf-8') as f:
        data=f.read()
        data=data.split('\n')
        for i in range(data.__len__()):
            data[i]=data[i].split(',')
    data.pop(0)
    return data
def write(thePath,data,dim=2,gap=','):
    '''

    :param thePath:
    :param data:二维数组
    :return:
    '''
    if dim==2:
        with open(thePath, 'w', encoding='utf-8') as f:
            length=data.__len__()
            for i in range(length):
                line=[str(x) for x in data[i]]
                f.writelines(gap.join(line))
                if i!=length-1:
                    f.writelines("\n")
    if dim==1:
        with open(thePath, 'w', encoding='utf-8') as f:
            data=[str(x) for x in data]
            f.writelines(gap.join(data))
    return True
if __name__=='__main__':
    data=read('D:\\大二下\\数据科学\\带作业\\抖音数据\\抖音数据\\all76.csv')
    data=data[:503]
    write('D:\\大二下\\数据科学\\带作业\\抖音数据\\抖音数据\\all76.csv',data)


    #for i in range(data.__len__()):
     #   run(data[i][0],i+1)




        
